package com.zadtek.shopmall;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.wxlib.util.SysUtil;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.SPUtil;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;


/**
 * Created by Zackv on 2018/4/2.
 */

public class App extends Application {

    private static Application application;
    public static YWIMKit mIMKit;
    public static final String APP_ID = "wxcdf35b000d6e3884";
    public static IWXAPI iwxapi;
    private static List<Activity> activities = new LinkedList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        initYW();
        Const.init(this);
        addLifecycle();
        initWXApi();
    }

    private void initYW() {
        final String APP_KEY = "23015524";
        //必须首先执行这部分代码, 如果在":TCMSSevice"进程中，无需进行云旺（OpenIM）和app业务的初始化，以节省内存;
        SysUtil.setApplication(this);
        if(SysUtil.isTCMSServiceProcess(this)){
            return;
        }
        //第一个参数是Application Context
        //这里的APP_KEY即应用创建时申请的APP_KEY，同时初始化必须是在主进程中
        if(SysUtil.isMainProcess()){
            YWAPI.init(application, APP_KEY);
        }
        boolean isLogin = (boolean) SPUtil.getData(this, Const.IsLogin,false);
        if(isLogin){
            Const.loginYM();
        }
    }

    private void initWXApi(){
        iwxapi = WXAPIFactory.createWXAPI(this,APP_ID,true);
        iwxapi.registerApp(APP_ID);
    }

    public static Application getApplication(){
        return application;
    }

    public static void finishAllActivity() {
        if (activities == null) {
            return;
        }
        for (Activity activity : activities) {
            activity.finish();
        }
        activities.clear();
    }

    private void addLifecycle(){
        application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                activities.add(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                if(activities.contains(activity)){
                    activities.remove(activity);
                }
            }
        });
    }

}
